/**
 * @ClassName: SysPromotionDao
 * @author linsen
 * @date 2016-10-20
 */
package com.fzrskj.cloud.free.sys.dao;

import com.fzrskj.cloud.free.core.tool.CObjectTool;
import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.core.utils.CloudDbDrvUtils;
import com.fzrskj.cloud.free.sys.bean.SysPromotion;
import ms.core.consts.SysErr;
import ms.core.db.IDbDrv;
import ms.core.object.CObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class SysPromotionDao extends CObject {
    //private declarations
    private IDbDrv db = null;

    //constructor function to initialize database access object
    public SysPromotionDao() {
        db = CloudDbDrvUtils.getDbUtilsDrv();
    }

    //to free resource
    @Override
    public void dispose() {
        db.closeConnection();
    }

    //to add new record to database by bean
    public int addSysPromotion(SysPromotion obj) {
        //代理商数据隔离
        obj.setAgentId(CloudContextUtils.getAgentId());
        String sql = "insert into sys_promotion(productCode,chargeId,applyTime,applyType,saleType," +
                "saleValue,agentId,plusValue1,plusValue2)" +
                "values(?,?,?,?,?, ?,?,?,?);";
        Object[] prm = {obj.getProductCode(), obj.getChargeId(), obj.getApplyTime(), obj.getApplyType(), obj.getSaleType(),
                obj.getSaleValue(), obj.getAgentId(), obj.getPlusValue1(), obj.getPlusValue2()};
        int ret = db.executeSql(sql, prm);
        String code = db.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }
        this.setErrCode(code);
        return ret;
    }

    //to add new record to database by map
    public int addSysPromotion(Map<String, Object> values) {
        //代理商数据隔离
        values.put("agentId", CloudContextUtils.getAgentId());

        int ret = db.insertRecord("sys_promotion", values);
        String code = db.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }
        this.setErrCode(code);
        return ret;
    }

    //to delete table record by id
    public int delSysPromotion(int id) {
        String sql = "delete from sys_promotion where id=?";

        Object[] params = {id};
        //代理商数据隔离
        sql += " and agentId=? ";
        params = CObjectTool.objectArrAddValue(params, CloudContextUtils.getAgentId());

        int ret = db.executeSql(sql, params);
        String code = db.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }
        this.setErrCode(code);
        return ret;
    }

    //to delete table record by cdt
    public int delSysPromotion(Map<String, Object> cdt) {
        //代理商数据隔离
        cdt.put("agentId", CloudContextUtils.getAgentId());

        int ret = db.deleteRecord("sys_promotion", cdt);
        String code = db.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }
        this.setErrCode(code);
        return ret;
    }

    //to modify table record by values and condition
    public int modSysPromotion(Map<String, Object> values, Map<String, Object> cdt) {
        //代理商数据隔离
        cdt.put("agentId", CloudContextUtils.getAgentId());

        int ret = db.modifyRecord("sys_promotion", values, cdt);
        String code = db.getErrCode();
        this.setErrCode(code);
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }

        return ret;
    }

    //to query sys_promotion item for detail
    public SysPromotion querySysPromotion(int id) {
        String sql = "select * from sys_promotion where id=?";

        Object[] params = {id};
        //代理商数据隔离
        sql += " and agentId=? ";
        params = CObjectTool.objectArrAddValue(params, CloudContextUtils.getAgentId());

        SysPromotion ret = db.queryForBean(sql, SysPromotion.class, params);
        String code = db.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }
        this.setErrCode(code);
        return ret;
    }

    //to query SysPromotion record count
    public int querySysPromotion(Map<String, Object> cdt) {
        //代理商数据隔离
        cdt.put("agentId", CloudContextUtils.getAgentId());

        int ret = db.queryRecordCount("sys_promotion", cdt);
        this.setErrCode(db.getErrCode());
        return ret;
    }

    //to query SysPromotion for record list with condition,if page or pagesize less than 0, means without page
    public List<SysPromotion> querySysPromotionList(Map<String, Object> cdt, int page, int pagesize) {
        //代理商数据隔离
        cdt.put("agentId", CloudContextUtils.getAgentId());

        List<SysPromotion> ret = db.queryForBeans("sys_promotion", "*", "id desc", SysPromotion.class, cdt, page, pagesize);
        String code = db.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }
        this.setErrCode(code);
        return ret;
    }

    //自定义方法(非生成)
    //获取产品促销列表
    public List<SysPromotion> getSysPromotionList(Map<String, Object> cdt) {
        //代理商数据隔离
        if (!cdt.containsKey("agentId")) {
            cdt.put("agentId", CloudContextUtils.getAgentId());
        }

        List<Object> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder("select * from sys_promotion where 1=1 ");
        for (Map.Entry<String, Object> c : cdt.entrySet()) {
            sb.append(" and " + c.getKey() + "=?");
            list.add(c.getValue());
        }

        Object[] params = new Object[list.size()];
        for (int i = 0; i < list.size(); i++) {
            params[i] = list.get(i);
        }
        sb.append(" order by id asc");
        List<SysPromotion> lists = db.queryForBeans(sb.toString(), SysPromotion.class, params);
        this.setErrCode(db.getErrCode());
        return lists;
    }

    /**
     * 查询促销消息.
     * @param cdt 查询条件.
     * @return 促销消息.
     */
    public SysPromotion queryPromotion(Map<String, Object> cdt) {
        return this.queryPromotionAppointAgentId(cdt, CloudContextUtils.getAgentId());
    }

    /**
     * 指定代理商查询促销消息.
     * @param cdt 查询条件.
     * @param agentId 代理商ID.
     * @return 促销消息.
     */
    public SysPromotion queryPromotionAppointAgentId(Map<String, Object> cdt, Integer agentId) {
        //代理商数据隔离
        cdt.put("agentId", agentId);

        List<Object> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder("select * from sys_promotion where 1=1 ");
        for (Map.Entry<String, Object> c : cdt.entrySet()) {
            sb.append(" and " + c.getKey() + "=?");
            list.add(c.getValue());
        }

        Object[] params = new Object[list.size()];
        for (int i = 0; i < list.size(); i++) {
            params[i] = list.get(i);
        }

        SysPromotion obj = db.queryForBean(sb.toString(), SysPromotion.class, params);
        this.setErrCode(db.getErrCode());
        return obj;
    }
}
